using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Xml;
using mock_ehr_lib.Model.Infobutton;

namespace mock_ehr_lib.Model.Summary
{
    public class ProblemItem : IInfobuttonProvider
    {
        private List<InfobuttonParameter> _infobuttonParameters;
        public string Description { get; set; }
        public DateTime? StartDate { get; set; }
        public DateTime? EndDate { get; set; }
        public string Status { get; set; }

        public ProblemItem()
        {
            _infobuttonParameters = new List<InfobuttonParameter>();
        }

        public static IEnumerable<ProblemItem> FromCCD(XmlDocument ccdXml, XmlNamespaceManager namespaceManager)
        {
            var problemItems = new List<ProblemItem>();
            // NOTE this XPath describes problems and functional status
            const string problemsXPath = "//ns:observation[ns:templateId[@root='2.16.840.1.113883.10.20.1.28']]";
            var problemNodes = ccdXml.SelectNodes(problemsXPath, namespaceManager);
            if (problemNodes != null)
            {
                var problemNodeEnumerator = problemNodes.GetEnumerator();
                if (problemNodeEnumerator != null)
                {
                    while (problemNodeEnumerator.MoveNext())
                    {
                        var current = problemNodeEnumerator.Current as XmlNode;
                        var descriptionNode = current.SelectSingleNode("./ns:value/@displayName", namespaceManager);
                        var description = descriptionNode == null ? null : descriptionNode.Value;
                        var statusNode =
                            current.SelectSingleNode("./ns:entryRelationship/ns:observation/ns:value/@displayName",
                                                     namespaceManager);
                        var status = statusNode == null ? null : statusNode.Value;
                        var problemItem = new ProblemItem { Description = description, Status = status };
                        problemItems.Add(problemItem);
                    }
                }
            }
            return problemItems;
        }


        public InfobuttonParameter[] GetParameters()
        {
            return new InfobuttonParameter[0];
        }
    }
}